package bookstudytest.singleton;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: Lee
 * @Date: 2018/6/11 14:54
 * @Description: 注册单例类 应该也是线程安全的
 */
public class RegistDemo {
    private RegistDemo(){

    }
    private static Map<String,RegistDemo> map = new HashMap<String,RegistDemo>();
    static {
        RegistDemo registDemo = new RegistDemo();
        map.put(registDemo.getClass().getName(),registDemo);
    }
    public static RegistDemo getInstace(String name){
        if (name==null){
            name = RegistDemo.class.getName();
        }
        if (map.get(name)==null){
            try {
                map.put(name, (RegistDemo) Class.forName(name).newInstance());
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return map.get(name);
    }
    public String about() {
        return "Hello, I am RegSingleton.";
    }
}
